//
//  CZComposeView.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit

class CZComposeView: UIView {
    
    //懒加载控件
    fileprivate lazy var bgImgaeView: UIImageView = {
        
        //把图片改成模糊效果
        let image = self.getScreenShot().applyLightEffect()
        
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
    //标语
    fileprivate lazy var sloganImageView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置大小
        size = CGSize(width: CZSCREEN_W, height: CZSCREEN_H)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //搭建界面
    private func setupUI(){
        
        addSubview(bgImgaeView)
        addSubview(sloganImageView)
        
        sloganImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(bgImgaeView)
            make.top.equalTo(bgImgaeView).offset(100)
        }
        
        addChildButton()
        
    }
    
    //添加6个子按钮方法
    private func addChildButton() {
        
        //按钮宽度
        let buttonWidth: CGFloat = 80
        
        //按钮高度
        let buttonHight: CGFloat = 110
        
        //计算每个按钮的间距
        let buttonMargin: CGFloat = (width - 3 * buttonWidth) / 4
        
        for i in 0..<6 {
            
            //计算列的索引
            let colIndex = i % 3
            
            //计算行的索引
            let rowIndex = i / 3
            
            let button = CZComposeMenuButton()
            
            button.setImage(UIImage(named: "tabbar_compose_photo"), for: UIControlState.normal)
            
            button.setTitle("文字", for: UIControlState.normal)
            
            //设置大小
            button.size = CGSize(width: buttonWidth, height: buttonHight)
            
            //设置X，Y坐标
            button.x = CGFloat(colIndex) * buttonWidth + CGFloat(colIndex + 1) * buttonMargin
            button.y = CGFloat(rowIndex) * buttonMargin + CGFloat(rowIndex) * buttonHight
         
            addSubview(button)
        }
    }
    
    //截取当前屏幕的图片
    func getScreenShot() ->UIImage {
        
        //  获取当前 主window
        let window = UIApplication.shared.keyWindow!
        
        //  开启图片上下文
        UIGraphicsBeginImageContext(window.size)
        
        let rect = CGRect(origin: CGPoint.zero, size: window.size)
        
        //  根据指定区域绘制
        //  afterScreenUpdates 是否等到屏幕更新完成还以后在绘制
        window.drawHierarchy(in: rect, afterScreenUpdates: false)
        
        //  通过图片上下文获取绘制的图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //  关闭图片上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        removeFromSuperview()
    }
}
